﻿using Newtonsoft.Json;
using NWMES.GetResourceSfcServiceService;
using System;
using System.Net;

namespace NWMES.Mes
{
    public class MesGetResourceSfcServiceService : MESBaseService
    {
        static string DefaultApiSection = "GetResourceSfcServiceService";
        public static NetworkCredential networkCredential = null;
        public static MesGetResourceSfcServiceService Params = null;

        static MesGetResourceSfcServiceService()
        {
            Params = config[DefaultApiSection].ToObject<MesGetResourceSfcServiceService>();
            networkCredential = new NetworkCredential(Params.UserName, Params.Password);
        }

        /// <summary>
        /// 根据SFC获取资源号
        /// </summary>
        /// <param name="sfc"></param>
        /// <param name="ApiSection"></param>
        /// <returns></returns>
        public static MesResult RequestMes(string sfc, 
           string ApiSection)
        {
            GetResourceSfcServiceService.GetResourceSfcServiceService ResourceSfcService;
            GetResourceSfcRequest Request;

            if (!string.IsNullOrEmpty(ApiSection))
            {
                Params = config[ApiSection].ToObject<MesGetResourceSfcServiceService>();
                networkCredential = new NetworkCredential(Params.UserName, Params.Password);
            }
            else
            {
                ApiSection = DefaultApiSection;
            }
            string ErrorMessage = Params.ParameterVerification("ModeProcessSFC");
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return new MesResult(-97, ErrorMessage);
            }
            SetRequestParams(out ResourceSfcService, out Request);

            //传入sfc
            Request.sfc = sfc;

            matchResourceSfcServiceResponse Response = new matchResourceSfcServiceResponse();
            MesResult result = MesResult.InitMesResult(JsonConvert.SerializeObject(Request));
            matchResourceSfcService mrss = new matchResourceSfcService();
            mrss.Request = Request;

            try
            {
                Response = ResourceSfcService.matchResourceSfcService(mrss);

                result.EndTime = DateTime.Now;
                var ret = Response.@return;
                result.Message = ret?.message;
                result.Code = ret.code;
                result.Success = result.Code == 0;
                result.ResponeJson = JsonConvert.SerializeObject(Response);
                result.Respone = Response;
                result.ret = ret;
            }
            catch (Exception ex)
            {
                result.Code = -103;
                result.Message = ex.Message + ";堆栈信息：" + ex.StackTrace;
            }
            SaveMesLog(ApiSection, result);
            return result;
        }

        private static void SetRequestParams(out GetResourceSfcServiceService.GetResourceSfcServiceService ResourceSfcService, 
            out GetResourceSfcRequest Request)
        {
            ResourceSfcService = new GetResourceSfcServiceService.GetResourceSfcServiceService();
            ResourceSfcService.Url = Params.Url;
            ResourceSfcService.Timeout = Params.Timeout;
            ResourceSfcService.Credentials = networkCredential;
            ResourceSfcService.PreAuthenticate = true;
            Request = new GetResourceSfcRequest();
            Request.operation = Params.Operation;
            Request.operationRevesion = Params.OperationRevision;
            Request.site = Params.Site;

        }
    }
}
